﻿#include "DAVariantValueNodeWidget.h"
#include "ui_DAVariantValueNodeWidget.h"

DAVariantValueNodeWidget::DAVariantValueNodeWidget(DAAbstractNodeGraphicsItem *item, QWidget *parent)
    : DAAbstractNodeWidget(item, parent), ui(new Ui::DAVariantValueNodeWidget)
{
    ui->setupUi(this);
}

DAVariantValueNodeWidget::~DAVariantValueNodeWidget()
{
    delete ui;
}

void DAVariantValueNodeWidget::apply()
{
    QString name = ui->lineEditName->text();
    QString vstr = ui->lineEditValueText->text();
    QString typestr = ui->comboBoxType->currentText();

    if ("string" == typestr) {
        typestr = "QString";
    } else if ("point" == typestr) {
        typestr = "QPoint";
    } else if ("pointF" == typestr) {
        typestr = "QPointF";
    } else if ("time" == typestr) {
        typestr = "QTime";
    } else if ("datetime" == typestr) {
        typestr = "QDateTime";
    } else if ("size" == typestr) {
        typestr = "QSize";
    } else if ("color" == typestr) {
        typestr = "QColor";
    }
    QVariant v = DADataPackage::stringToVariant(vstr, typestr);

    qDebug() << "typestr:" << typestr << "\nvstr:" << vstr;
    if (!v.isValid()) {
        ui->labelInfo->setText(tr("variant is invalid!"));
        return;
    }
    m_data.setValueItem("value", DADataItem(v, name));
    ui->labelInfo->setText(tr(""));
    DAAbstractNode *n = getNode();

    if (n) {
        n->input("_value", m_data.copy());
    }
}

void DAVariantValueNodeWidget::on_comboBoxType_currentIndexChanged(const QString &arg1)
{
    Q_UNUSED(arg1);
    apply();
}

void DAVariantValueNodeWidget::on_lineEditValueText_editingFinished()
{
    apply();
}
